<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-01-04 15:43:50
 * @LastEditors: sunkaiyuan sunkaiyuan@namenode.com
 * @Description: 
 */

namespace App\Http\Services\System;

use App\Http\Services\Service;

use App\Repositories\TableReportRepositorie;
use App\Repositories\TableRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class TableReportService extends Service
{

    public $table;
    public function __construct(TableReportRepositorie $repository, TableRepository $table)
    {
        $this->repository = $repository;
        $this->table = $table;
    }

    public function list()
    {
        $data = $this->repository->condition()->getGrid();
        return $data;
    }
    public function info()
    {

        $request = request();
        if ($request->has("id"))
            $this->repository->whereId($request->id);
        else if ($request->has("code"))
            $this->repository->whereCode($request->code);
        else {
            fail('参数不正确');
        }
        return $this->repository->first();
    }

    protected function save()
    {
        $request = request();
        $table_id = $request->input("table_id");
        $data = $this->table->whereId($table_id)->first();
        if ($data == null)
            fail("未找到对应报表");

        $name = date("YmdHis") . rand(100, 999) . ".grf";
        $path = "{$data->code}/{$name}";
        // Storage->put('test.txt', 'this is test');

        Storage::disk("report")->put(
            "{$path}",
            '{
                "Version":"6.8.5.6",
                "Font":{
                "Name":"宋体",
                "Size":75000,
                "Weight":400,
                "Charset":134
            },
            "Printer":{
            }
            }'
        );
        $report = $this->repository->create([
            'filepath' => "{$path}",
            'filename' => $name,
            "table_id" => $table_id
        ]);
        return $report;
    }
    public function saveReport()
    {
        $request = request();

        $id = $request->input("id");

        $data = $this->repository->whereId($id)->first();

        if ($data == null)
            fail("未找到报表");
        // $path = Storage::saveFile("{$data->gridtable->code}", $data->filename);
        Storage::disk("report")->put("{$data->filepath}", $request->getContent());
        return $data->url;
    }
}